package com.mengdie.shuidi.ui.interfaces;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.mengdie.shuidi.AppContext;
import com.mengdie.shuidi.OkHeader;
import com.orhanobut.logger.Logger;

import org.json.JSONObject;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by apple on 17/4/12.
 */

public class CheckUrlPresent {
    private CheckUrlInterface mCheckUrlInterface;

    public CheckUrlPresent(CheckUrlInterface checkUrlInterface) {
        mCheckUrlInterface = checkUrlInterface;
    }

    public void check(){
        String baseUrl = AppContext.getInstance().getBaseUrl();
        String substring = baseUrl.substring(0,baseUrl.indexOf("api/"));
        Logger.e(substring);
        OkGo.getInstance().addCommonHeaders(OkHeader.getCommonHead(AppContext.getInstance()))
                .post(substring +"index.php/api/index/check")
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        mCheckUrlInterface.onError();
                    }

                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        try {
                            JSONObject jo=new JSONObject(s);
                            String ret=jo.getString("ret");
                            String msg=jo.getString("msg");
                            if (ret.equals("0")){
                                mCheckUrlInterface.onSuccess();
                            } else if(ret.equals("1")){

                            }
                        }catch (Exception e){}
                    }
                });
    }
}
